<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>iframe4</title>
  <style>
    body {
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <h2>我是4.html</h2>
  <p></p>
  <!-- 用于接受 1.html 使用 postMessage 发送过来的数据 -->
  <p class="result"></p>
  <!-- 设置一个按钮给 1.html 发送数据 -->
  <button class="postMessage">postMessage in 4.html ---> 62032</button>
</body>

</html>
<script>
  // 设置相同的一级域名
  // document.domain = 'gqs.com'
  document.getElementsByTagName('p')[0].innerText = window.location + '端口号不一样'

  // var global4Obj = {
  //   name: '李四'
  // }
  // let p = document.getElementById('value')
  let result = document.querySelector('.result')
  window.addEventListener('message', function (e) {
    result.innerText = e.data
  },false)

  let postMessageButton = document.querySelector('.postMessage')
  postMessageButton.addEventListener('click', function () {
    window.parent.postMessage('4.html发送过来的数据','http://127.0.0.1:22222/')
  }, false)

  // document.getElementById('btn').addEventListener('click', function () {
  //   const data = '内部往外部传递的数据'
  //   window.parent.postMessage(data,'http://127.0.0.1:12345/')
  // }, false)
</script>